﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;

namespace KPIS.GERP.GBMS.DAL
{
    [Serializable]
    public sealed class StrategyMissionDAL : IStrategyMissionDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public int Insert(StrategyMissionInfo strategyInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@code", strategyInfo.Code);
                dbManager.AddParameters(1, "@name", strategyInfo.Name);
                dbManager.AddParameters(2, "@descr", strategyInfo.Description);
                dbManager.AddParameters(3, "@status", SystemInfo._ActiveRecordStatus);

                strSQL = "INSERT INTO BGM_MAS_STRATEGY_MISSION(BGM_STRAT_MISSION_CODE, BGM_STRAT_MISSION_NAME,"
                        +" BGM_STRAT_MISSION_DESCR, RECORD_STATUS)"
                        +" VALUES (@code, @name, @descr, @status)";

               return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public int Update(StrategyMissionInfo strategyInfo)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@seq", strategyInfo.Sequence);
                dbManager.AddParameters(1, "@code", strategyInfo.Code);
                dbManager.AddParameters(2, "@name", strategyInfo.Name);
                dbManager.AddParameters(3, "@descr", strategyInfo.Description);
                dbManager.AddParameters(4, "@status", SystemInfo._ActiveRecordStatus);

                strSQL = "UPDATE BGM_MAS_STRATEGY_MISSION"
                + " SET BGM_STRAT_MISSION_CODE = @code"
                    + ", BGM_STRAT_MISSION_NAME = @name"
                    + ", BGM_STRAT_MISSION_DESCR = @descr"
                    + ", RECORD_STATUS = @status"
                + " WHERE BGM_STRAT_MISSION_SEQ = @seq";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public int Delete(int seq)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@status", SystemInfo._DeleteRecordStatus);

                return dbManager.ExecuteNonQuery(CommandType.Text, "UPDATE BGM_MAS_STRATEGY_MISSION SET RECORD_STATUS = @status WHERE BGM_STRAT_MISSION_SEQ = @seq");
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public IList<StrategyMissionInfo> Search(StrategyMissionInfo strategyInfo)
        {
            List<StrategyMissionInfo> stratType = new List<StrategyMissionInfo>();
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@seq", strategyInfo.Sequence);
                dbManager.AddParameters(1, "@code", strategyInfo.Code);
                dbManager.AddParameters(2, "@name", strategyInfo.Name);
                dbManager.AddParameters(3, "@descr", strategyInfo.Description);
                dbManager.AddParameters(4, "@status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT BGM_STRAT_MISSION_SEQ, BGM_STRAT_MISSION_CODE, BGM_STRAT_MISSION_NAME, BGM_STRAT_MISSION_DESCR, RECORD_STATUS"
                    + " FROM BGM_MAS_STRATEGY_MISSION"
                    + " WHERE RECORD_STATUS not like @status"
                        + ((strategyInfo.Sequence > 0) ? " AND BGM_STRAT_MISSION_SEQ = @seq" : "")
                        + ((strategyInfo.Code.Length > 0) ? " AND BGM_STRAT_MISSION_CODE = @code" : "")
                        + ((strategyInfo.Name.Length > 0) ? " AND BGM_STRAT_MISSION_NAME = @name" : "")
                        + ((strategyInfo.Description.Length > 0) ? " AND BGM_STRAT_MISSION_DESCR = @descr" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    StrategyMissionInfo stratRecord = new StrategyMissionInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.IsDBNull(3) ? "" : rdr.GetString(3), rdr.GetString(4), null);
                    stratType.Add(stratRecord);
                }

                return stratType;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public bool CheckDuplicateCode(string code, string currentSeq)
        {
            string strSQL;

            bool result = false;

            try
            {

                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@deletestatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_strat_mission_code", code);
                dbManager.AddParameters(2, "@bgm_strat_mission_seq", currentSeq);

                strSQL = @"SELECT bgm_strat_mission_code 
                            FROM bgm_mas_strategy_mission
                            WHERE bgm_strat_mission_code = @bgm_strat_mission_code "
                          + (String.IsNullOrEmpty(currentSeq) 
                            ? String.Empty
                            : " AND bgm_strat_mission_seq not like @bgm_strat_mission_seq ")
                          + " AND record_status not like @deletestatus";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    result = true;
                    break;
                }
            }
            catch (Exception Error)
            {
            }
                finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return result;
        }
    }
}